package persist

import (
"log"
"gopkg.in/olivere/elastic.v5"
	"context"
	"gxgeek.com/gxgeek/learngo/crawler/engine"
	"errors"
)

func ItemSaver(index string)  (chan engine.Item,error) {
	out := make(chan engine.Item)
	client, err := elastic.NewClient(
		//Must turn off sniff in docker
		elastic.SetSniff(false))
	if err != nil {
		return nil,err
	}

	go func() {
		itemCount := 0
		for {
			item := <-out
			log.Printf("ItemSaver Got item #%d: %v", itemCount, item)
			itemCount++
			err := Save(item,client,index)
			if err !=nil {
				log.Printf("Item Saver :error  saving item %v : %v",item,err)
			}
		}
	}()
	return out,nil
}
func Save(item engine.Item,client *elastic.Client,
	index string) ( err error) {

	if item.Type == "" {
		return errors.New("Must supply type")
	}
	service := client.Index().
		Index(index).
		Type(item.Type).
		BodyJson(item)
	if item.Id != "" {
		service.Id(item.Id)
	}
	_,err = service.
		Do(context.Background())
	if err !=nil {
		return err
	}
	return nil
}
